<?php
namespace app\admin\controller;

class Page extends Common
{
    private $obj;
    public function _initialize()
    {
        parent::_initialize();
        $this->cate_obj = model('PageCategory');
        $this->article_obj = model('PageArticle');
    }

    /**
     * 单页分类
     * @return \think\response\View
     */
    public function category()
    {
        $data = $this->cate_obj->all();
        $this->assign('data', $data);
        return view();
    }

    /**
     * 保存分类信息
     * @return \think\response\View
     */
    public function save_category() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['name'] = input('post.name', '');

            /**
             * 验证数据
             */
            $validate = validate('Category');
            if(!$validate->check($data)) {
                $this->error($validate->getError());
            }

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = $this->cate_obj->save($data, $where);

            // 清理缓存
            \app\api\service\Base::clear_cache();

            if ($rs) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        } else{
            $data = [];
            if ($id > 0) {
                $data = $this->cate_obj->get(['id' => $id]);
            }
            $this->assign('data', $data);
            return view();
        }
    }

    /**
     * 单页内容
     * @return \think\response\View
     */
    public function article()
    {
        $category_id = input('param.cid', 0);
        $where = [];
        if ($category_id > 0) {
            $where['page_category_id'] = $category_id;
        }
        $data = $this->article_obj->paginate();
        $this->assign('data', $data);

        return view();
    }

    /**
     * 保存文章内容
     * @return \think\response\View
     */
    public function save_article() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['title'] = input('post.title', '');
            $data['author'] = input('post.author', '');
            $data['page_category_id'] = input('post.page_category_id', '');;
            $data['desc'] = input('param.desc', '');
            $data['content'] = input('param.content', '');
            $data['hits'] = input('post.hits', '');
            if ($create_time = input('post.create_time')) {
                $data['create_time'] = strtotime($create_time);
            }

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = $this->article_obj->save($data, $where);

            // 清理缓存
            \app\api\service\Base::clear_cache();

            if ($rs) {
                $this->success('保存成功', '');
            } else {
                $this->error('保存失败');
            }
        } else{
            $data = [];
            if ($id > 0) {
                $data = $this->article_obj->get(['id' => $id]);
            }
            $this->assign('data', $data);

            $cate = model('PageCategory')->all();
            $this->assign('cate', $cate);

            return view();
        }
    }
}
